package com.ruoyi.site.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.site.domain.OrderDetails;
import com.ruoyi.site.service.IOrderDetailsService;
import com.ruoyi.site.service.IStripeService;
import com.ruoyi.site.vo.PaymentIntentRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/site/payment")
public class PaymentController extends BaseController {

    @Autowired
    private IStripeService stripeService;

    @Autowired
    private IOrderDetailsService orderDetailsService;

    /**
     * 为订单创建支付意图
     */
    @PostMapping("/create-intent")
    public AjaxResult createPaymentIntent(@RequestBody PaymentIntentRequest request) {
        // (此处应添加校验，确保该 orderId 属于当前登录用户)
        OrderDetails order = orderDetailsService.selectOrderDetailsByOrderId(request.getOrderId());
        if (order == null) {
            return AjaxResult.error("订单不存在");
        }
        if (!"PENDING_PAYMENT".equals(order.getStatus())) {
            return AjaxResult.error("订单状态不正确，无法支付");
        }

        return stripeService.createPaymentIntent(order);
    }
}